'use strict';
const Hogan = require('hogan.js');
const conf = {
    serverHost : '//118.25.17.117:8888'
};
const _mm = {
    
    // 网络请求
    request : function(param){
        var _this = this;
        $.ajax({
            type        : param.method  || 'get',
            url         : param.url     || '',
            dataType    : param.type    || 'json',
            data        : param.data    || '',
            contentType : param.contentType || 'application/x-www-form-urlencoded;charset=UTF-8',
            traditional : param.contentType  || false,
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            // beforeSend: function(request) {
            //     // console.log('request getToken:',getToken());
            //     if(getToken()){
            //         request.setRequestHeader("Authorization", getToken());
            //     }else{
            //         request.setRequestHeader("Authorization", '');
            //     }
            // },
            success     : function(res){
                // 请求成功
                if(200 === res.status){
                    if(res.errorCode!==0){
                    // console.log(res.token)
                     // _this.doLogin();
                        typeof param.error === 'function' && param.error(res);
                    }else{
                        typeof param.success === 'function' && param.success(res);
                    }

                 }else{
                    typeof param.error === 'function' && param.error(err);
                 }
            },
            error       : function(err){
                typeof param.error === 'function' && param.error(err);
            }
        });
    },
    // 获取服务器地址
    getServerUrl : function(path){
        if(!path||typeof path =='undefined'){
            path = '';
        }
        return conf.serverHost + path;
    },
    // 获取url参数
    getUrlParam : function(name){
        var reg     = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var result  = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    },
    urlHasString:function(str){
        let url = window.location.href;
        if (url.indexOf(str)>=0) {
            return true;
        }else{
            return false;
        }
    },
    // 渲染html模板
    renderHtml : function(htmlTemplate, data){
        var template    = Hogan.compile(htmlTemplate),
            result      = template.render(data);
        return result;
    },
    goHome : function(){
        window.location.href = '/index.html';
    },
    goPage: function(url){
        window.location.href = url;
    },
    goErrorPage:function(num){
        window.location.href = '/'+num+'.html';
    },
    // Array => Object
    serializeJsonObj(data){
        var o = {};
        var a = data;
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
};

module.exports = _mm;